<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/6/23
  Time: 17:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>批量代付接口调用DEMO</title>
    <meta content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width" name="viewport">
    <link rel="stylesheet" href="static/bootstrap-3.3.5/css/bootstrap.css"/>
    <script type="text/javascript" src="static/jquery/jquery-3.0.0.js"></script>
    <script type="text/javascript" src="static/bootstrap-3.3.5/js/bootstrap.js"></script>
    <script type="text/javascript" src="static/jquery.form/jquery.form.js"></script>
</head>
<body>

<div class="container">
    <fieldset>
        <legend>批量代付接口调用DEMO</legend>
    </fieldset>
    <form role="form" id="batch-profit-form" class="form-horizontal" enctype="multipart/form-data">
        <div class="form-group">
            <label for="merchantId" class="col-sm-2 control-label">商户标识：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="merchantId" id="merchantId" placeholder="请输入商户标识"
                       value="${batchProfit.merchantId}">
            </div>
        </div>
        <div class="form-group">
            <label for="appId" class="col-sm-2 control-label">商户应用标识：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appId" id="appId" placeholder="商户应用标识"
                       value="${batchProfit.appId}">
            </div>
        </div>
        <div class="form-group">
            <label for="batchNo" class="col-sm-2 control-label">批次号：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="batchNo" id="batchNo" placeholder="请输入批次号"
                       value="${batchProfit.batchNo}">
            </div>
        </div>
        <div class="form-group">
            <label for="totalAmount" class="col-sm-2 control-label">总金额：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="totalAmount" id="totalAmount" placeholder="请输入总金额"
                       value="${batchProfit.totalAmount}">
            </div>
        </div>
        <div class="form-group">
            <label for="countRecord" class="col-sm-2 control-label">总笔数：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="countRecord" id="countRecord" placeholder="请输入总笔数"
                       value="${batchProfit.countRecord}">
            </div>
        </div>
        <div class="form-group">
            <label for="uploadFile" class="col-sm-2 control-label">请求文件：</label>
            <div class="col-sm-8 row">
                <input class="col-sm-4" type="file" name="uploadFile" id="uploadFile"
                       placeholder="请输入请求文件">
                <span id="error-info" class="col-sm-3" style="color: red;"></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" id="batch-profit-btn" class="btn btn-info">提交</button>
            </div>
        </div>
        <div class="form-group">
            <label for="pay-result" class="col-sm-2 control-label">支付结果：</label>
            <div class="col-sm-8">
                <textarea id="pay-result" class="col-sm-10 form-control" rows="5"></textarea>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/javascript">
    $(function () {
        if (!$("#uploadFile").val())
            $("#batch-profit-btn").attr("disabled", "disabled");

        $("#uploadFile").on("change", function () {
            $("#uploadFile").val() ?
                    $("#batch-profit-btn").removeAttr("disabled") : $("#batch-profit-btn").attr("disabled", "disabled");
        });

        $("#batch-profit-btn").click(function () {
            var file = $("#uploadFile").val();
            var filename = file.replace(/.*(\/|\\)/, "");
            var fileExt = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';
            console.log(filename);
            console.log(fileExt);
            console.log(fileExt[0]);
            if (fileExt[0] == "txt" || fileExt[0] == "json") {
                var formData = new FormData();
                formData.append("merchantId", $("#merchantId").val());
                formData.append("appId", $("#appId").val());
                formData.append("batchNo", $("#batchNo").val());
                formData.append("totalAmount", $("#totalAmount").val());
                formData.append("countRecord", $("#countRecord").val());
                formData.append("uploadFile", $("#uploadFile")[0].files[0]);

                $.ajax({
                    url: "batch_profit/do_batch_profit",
                    type: "post",
                    dataType: "json",
                    processData: false,  // 告诉jQuery不要去处理发送的数据
                    contentType: false,
                    data: formData,
                    success: function (data) {
                        console.log(data);
                        $("#pay-result").html(JSON.stringify(data.data[0]));
                    },
                    error: function (e) {
                        $("#pay-result").html(e);
                    }
                });
            } else {
                $("#error-info").html("只支持后缀为 txt、 json 格式的文件")
            }
        });
    });
</script>
</html>
